<!DOCTYPE HTML>
<html lang="en">
	<head>
		<title>three.js webgl - animation - morph targets</title>
		<meta charset="utf-8">
		<style type="text/css">
			body {
				background:#000;
				color:#fff;
				padding:0;
				margin:0;
				font-weight: bold;
				overflow:hidden;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				color: #ffffff;
				padding: 5px;
				font-family: Monospace;
				font-size: 13px;
				text-align: center;
				z-index:100;
			}

			#ctrl {
				position: absolute;
				top: 0px;
				left: 0px;
				width: 200px;
				color: #ffffff;
				padding: 5px;
				font-family: Monospace;
				font-size: 13px;
				z-index:100;
			}


			a { color:red }

		</style>
	</head>

	<body>
		<div id="info">
			<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - WebGL morph target example
		</div>


		<div id="ctrl">
			Use controls to change morph target influences:<br/>
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[ 0 ] = this.value/100;" />
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[ 1 ] = this.value/100;" />
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[ 2 ] = this.value/100;" />
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[ 3 ] = this.value/100;" />
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[ 4 ] = this.value/100;" />
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[ 5 ] = this.value/100;" />
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[ 6 ] = this.value/100;" />
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[ 7 ] = this.value/100;" />
		</div>


		<script type="text/javascript" src="../build/Three.js"></script>

		<script type="text/javascript" src="js/Detector.js"></script>
		<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
		<script type="text/javascript" src="js/Stats.js"></script>

		<script type="text/javascript">

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats;

			var camera, scene, renderer;

			var geometry, objects;

			var mouseX = 0, mouseY = 0;

			var mesh;
			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			document.addEventListener( 'mousemove', onDocumentMouseMove, false );

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
				camera.position.z = 500;


				scene = new THREE.Scene();
				scene.fog = new THREE.Fog( 0x000000, 1, 15000 );


				var light = new THREE.PointLight( 0xff2200 );
				light.position.set( 100, 100, 100 );
				scene.addLight( light );

				var light = new THREE.AmbientLight( 0x333333 );
				scene.addLight( light );


				var geometry = new THREE.CubeGeometry( 100, 100, 100 );
				var material = new THREE.MeshLambertMaterial( { color: 0xffffff, morphTargets: true } );

				// construct 8 blend shapes

				for ( var i = 0; i < geometry.vertices.length; i ++ ) {

					var vertices = [];

					for ( var v = 0; v < geometry.vertices.length; v ++ ) {

						vertices.push( new THREE.Vertex( geometry.vertices[ v ].position.clone() ) )

						if ( v === i ) {

							vertices[ vertices.length - 1 ].position.x *= 2;
							vertices[ vertices.length - 1 ].position.y *= 2;
							vertices[ vertices.length - 1 ].position.z *= 2;

						}

					}

					geometry.morphTargets.push( { name: "target" + i, vertices: vertices } );

				}

				mesh = new THREE.Mesh( geometry, material );

				scene.addChild( mesh );


				renderer = new THREE.WebGLRenderer( { clearColor:0x222222, clearAlpha: 1 } );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.sortObjects = false;
				container.appendChild( renderer.domElement );

			}

			function onDocumentMouseMove(event) {

				mouseX = ( event.clientX - windowHalfX );
				mouseY = ( event.clientY - windowHalfY ) * 2;

			}

			function animate() {

				requestAnimationFrame( animate );
				render();

			}

			function render() {

				mesh.rotation.y += 0.01;

				//mesh.morphTargetInfluences[ 0 ] = Math.sin( mesh.rotation.y ) * 0.5 + 0.5;

				//camera.position.x += ( mouseX - camera.position.x ) * .005;
				camera.position.y += ( - mouseY - camera.position.y ) * .01;

				renderer.render( scene, camera );

			}

			function log( text ) {

				var e = document.getElementById("log");
				e.innerHTML = text + "<br/>" + e.innerHTML;

			}

		</script>

	</body>
</html>
